﻿namespace Hidistro.Entities
{
    using Hidistro.Core;
    using Hidistro.Membership.Context;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Web;
    using System.Web.Caching;
    using System.Xml;
    using wnxd.javascript;

    public class wnxd_RegionInfo
    {
        public int Type { get; set; }
        public string Name { get; set; }
        public int Id { get; set; }
        public int Index { get; set; }
        public wnxd_RegionInfo Pinfo { get; set; }
    }
    public static class RegionHelper
    {
        //private static json json;
        //static RegionHelper()
        //{
        //    StreamReader stream = File.OpenText(HttpContext.Current.Server.MapPath("~/config/wnxd.region.json"));
        //    json = new json(stream.ReadToEnd());
        //}
        private static XmlNode FindNode(int id)
        {
            string str = id.ToString(CultureInfo.InvariantCulture);
            string xpath = string.Format("//county[@id='{0}']", str);
            XmlDocument regionDocument = GetRegionDocument();
            XmlNode node = regionDocument.SelectSingleNode(xpath);
            if (node != null)
            {
                return node;
            }
            xpath = string.Format("//city[@id='{0}']", str);
            node = regionDocument.SelectSingleNode(xpath);
            if (node != null)
            {
                return node;
            }
            xpath = string.Format("//province[@id='{0}']", str);
            node = regionDocument.SelectSingleNode(xpath);
            if (node != null)
            {
                return node;
            }
            return null;
        }

        public static Dictionary<int, string> GetAllProvinces()
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            XmlNodeList list = GetRegionDocument().SelectNodes("//province");
            foreach (XmlNode node in list)
            {
                dictionary.Add(int.Parse(node.Attributes["id"].Value), node.Attributes["name"].Value);
            }
            //string[] p = json["0"];
            //for (int i = 0; i < p.Length; i++)
            //{
            //    string[] arr = p[i].Split('|');
            //    dictionary.Add(int.Parse(arr[1]), arr[0]);
            //}
            return dictionary;
        }



        private static Dictionary<int, string> GetChildList(string xpath)
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            XmlNode node = GetRegionDocument().SelectSingleNode(xpath);
            foreach (XmlNode node2 in node.ChildNodes)
            {
                dictionary.Add(int.Parse(node2.Attributes["id"].Value), node2.Attributes["name"].Value);
            }
            return dictionary;
        }

        public static Dictionary<int, string> GetCitys(int provinceId)
        {
            //Dictionary<int, string> dictionary = new Dictionary<int, string>();
            //string p = "0_" + provinceId;
            //string[] list = json[p];
            //for (int i = 0; i < list.Length; i++)
            //{
            //    string[] arr = list[i].Split('|');
            //    dictionary.Add(int.Parse(arr[1]), arr[0]);
            //}
            //return dictionary;
            return GetChildList(string.Format("root/region/province[@id='{0}']", provinceId.ToString(CultureInfo.InvariantCulture)));
        }

        public static Dictionary<int, string> GetCountys(int cityId)
        {
            return GetChildList(string.Format("root/region/province/city[@id='{0}']", cityId.ToString(CultureInfo.InvariantCulture)));
        }

        //public static Dictionary<int, string> GetCountys(int provinceId, int cityId)
        //{
        //    Dictionary<int, string> dictionary = new Dictionary<int, string>();
        //    string p = "0_" + provinceId + "_" + cityId;
        //    string[] list = json[p];
        //    for (int i = 0; i < list.Length; i++)
        //    {
        //        string[] arr = list[i].Split('|');
        //        dictionary.Add(int.Parse(arr[1]), arr[0]);
        //    }
        //    return dictionary;
        //}

        public static string GetFullPath(int currentRegionId)
        {
            XmlNode node = FindNode(currentRegionId);
            if (node == null)
            {
                return string.Empty;
            }
            string str = node.Attributes["id"].Value;
            for (XmlNode node2 = node.ParentNode; node2.Name != "region"; node2 = node2.ParentNode)
            {
                str = node2.Attributes["id"].Value + "," + str;
            }
            return str;
        }

        public static string GetFullRegion(int currentRegionId, string separator)
        {
            XmlNode node = FindNode(currentRegionId);
            if (node == null)
            {
                return string.Empty;
            }
            string str = node.Attributes["name"].Value;
            for (XmlNode node2 = node.ParentNode; node2.Name != "region"; node2 = node2.ParentNode)
            {
                str = node2.Attributes["name"].Value + separator + str;
            }
            return str;
        }

        public static Dictionary<int, string> GetProvinces(int regionId)
        {
            return GetChildList(string.Format("root/region[@id='{0}']", regionId.ToString(CultureInfo.InvariantCulture)));
        }

        public static XmlNode GetRegion(int regionId)
        {
            return FindNode(regionId);
        }

        //public static string JSON_FindNode(int id, out int index)
        //{
        //    string ret = null;
        //    string target = "|" + id;
        //    index = -1;
        //    foreach (KeyValuePair<string, json> o in json)
        //    {
        //        for (int i = 0; i < o.Value.length; i++)
        //        {
        //            string n = o.Value[i].Value;
        //            if (n.Substring(n.Length-target.Length) == target)
        //            {
        //                ret = o.Key;
        //                index = i;
        //                goto jmp;
        //            }
        //        }
        //    }
        //jmp:
        //    return ret;
        //}
        //public static wnxd_RegionInfo JSON_GetRegion(int regionId)
        //{
        //    int index;
        //    string n = JSON_FindNode(regionId, out index);
        //    if (n == null) return null;
        //    string[] arr = n.Split('_');
        //    wnxd_RegionInfo root = new wnxd_RegionInfo();
        //    wnxd_RegionInfo now = root;
        //    for (int i = arr.Length - 1; i >= 0; i--)
        //    {
        //        int id = int.Parse(arr[i]);
        //        now.Type = i;
        //        now.Index = index;
        //        string p = "";
        //        string nowPath = id.ToString();
        //        if (i > 0)
        //        {
        //            string[] tmp = new string[i];
        //            Array.Copy(arr, tmp, i);
        //            p = string.Join("_", tmp);
        //            nowPath = p + "_" + nowPath;
        //            now.Pinfo = new wnxd_RegionInfo();
        //        }
        //        string info = json[nowPath][index];
        //        string[] sa = info.Split('|');
        //        now.Id = int.Parse(sa[1]);
        //        now.Name = sa[0];
        //        now = now.Pinfo;
        //        index = id;
        //    }
        //    return root;
        //}

        private static XmlDocument GetRegionDocument()
        {
            XmlDocument document = HiCache.Get("FileCache-Regions") as XmlDocument;
            if (document == null)
            {
                string filename = HiContext.Current.Context.Request.MapPath(Globals.ApplicationPath + "/config/region.config");
                document = new XmlDocument();
                document.Load(filename);
                HiCache.Max("FileCache-Regions", document, new CacheDependency(filename));
            }
            return document;
        }

        public static int GetRegionId(string county, string city, string province)
        {
            string xpath = string.Format("//province[@name='{0}']", province);
            XmlNode node = GetRegionDocument().SelectSingleNode(xpath);
            if (node != null)
            {
                int num = int.Parse(node.Attributes["id"].Value);
                xpath = string.Format("//province[@id='{0}']/city[@name='{1}']", num, city);
                node = node.SelectSingleNode(xpath);
                if (node != null)
                {
                    num = int.Parse(node.Attributes["id"].Value);
                    xpath = string.Format("//city[@id='{0}']/county[@name='{1}']", num, county);
                    node = node.SelectSingleNode(xpath);
                    if (node != null)
                    {
                        num = int.Parse(node.Attributes["id"].Value);
                    }
                }
                return num;
            }
            return 0;
        }

        public static Dictionary<int, string> GetRegions()
        {
            return GetChildList("root");
        }

        public static int GetTopRegionId(int currentRegionId)
        {
            XmlNode node = FindNode(currentRegionId);
            if (node == null)
            {
                return 0;
            }
            int num = currentRegionId;
            for (XmlNode node2 = node.ParentNode; node2.Name != "region"; node2 = node2.ParentNode)
            {
                num = int.Parse(node2.Attributes["id"].Value);
            }
            return num;
        }

        public static Dictionary<int, string> GetMonthlyIncome()
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            string mi = "1000元以下|1000-2000元|2000-3000元|3000-5000元|5000-8000元|8000-10000元|10000元以上";
            string[] arr = mi.Split('|');
            for (int i = 0; i < arr.Length; i++)
            {
                dictionary.Add(i+1, arr[i]);
            }
            return dictionary;
        }
    }
}

